//
//  CXLTransitionAnimator.swift
//  weibo1990
//
//  Created by 曹晓龙 on 15/10/27.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit

///  modal 转场动画代理
protocol CXLTransitionPresentedDelegate :NSObjectProtocol{
    
    ///  转场动画时,替代的imageView
    ///
    ///  - parameter indexPath: 放大图片的索引
    func imageViewForTranstionPresented(indexPath:NSIndexPath) -> UIImageView
    
    ///  动画开始时,图片的初始位置
    func transitionPresentedStartFrame(indexPath:NSIndexPath) -> CGRect
    
    ///  动画结束时,图片的位置
    func transitionPresentedEndFrame(indexPath:NSIndexPath) -> CGRect
}

///  dismiss 转场动画代理
protocol CXLTransitionDismissDelegate :NSObjectProtocol{
    
    ///  dismiss 时 站位的imgView
    func imageViewForTranstionDismiss() ->UIImageView
    ///  dismiss 时,图片的索引
    func indexPathForTranstionDismiss() -> NSIndexPath
    
}

/// 自定义转场动画属性
class CXLTransitionAnimator: NSObject, UIViewControllerTransitioningDelegate{

    /// presented 代理属性
    weak var presentedDelegate:CXLTransitionPresentedDelegate?
    
    /// 传入modal的开始时索引
    var indexPath:NSIndexPath?
    
    /// dismiss 代理属性
    var dismissDelegate:CXLTransitionDismissDelegate?
    
    ///  设置转场动画代理所需要的属性
    ///
    ///  - parameter presentedDelegate: modal时遵守代理协议的对象
    ///  - parameter indexPath:         modal时图片的索引
    ///  - parameter presentedDelegate: dismiss时遵守代理协议的对象
    func setTransitonAnimatorDelegate(presentedDelegate:CXLTransitionPresentedDelegate?,
        indexPath:NSIndexPath?,
        dismissDelegate:CXLTransitionDismissDelegate?){
        
            self.presentedDelegate = presentedDelegate
            self.indexPath = indexPath
            self.dismissDelegate = dismissDelegate
    }
    
    /// 控制器跳转的标记
    private var isPresented = false
    
    ///  modal 控制器实现动画
    ///  - returns: 实现转场动画的对象--- 必须遵守 UIViewControllerAnimatedTransitioning
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        
        return self
    }
    
    ///  dismiss
    ///  - returns: 实现转场动画的对象--- 必须遵守 UIViewControllerAnimatedTransitioning
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    
        isPresented = false
    
        return self
    }
}

// MARK: - UIViewControllerAnimatedTransitioning
extension CXLTransitionAnimator : UIViewControllerAnimatedTransitioning{
    
    ///  转成动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 1
    }
    
    ///  动画的具体实现
    ///  - 实现了该方法,系统将不再进行动画的跳转,一切交给程序员自己管理
    ///  - parameter transitionContext: 转场动画的上下文
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? presentedTransition(transitionContext) : dismissTransition(transitionContext)
        
    }
    
    ///  dismiss 控制器的动画
    private func dismissTransition(transitionContext: UIViewControllerContextTransitioning){
        
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        fromView.removeFromSuperview()
        
        guard let dismissDelegate = dismissDelegate, presentedDelegate = presentedDelegate else{
            return
        }
        
        let imgView = dismissDelegate.imageViewForTranstionDismiss()
        transitionContext.containerView()?.addSubview(imgView)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            let indexPath = dismissDelegate.indexPathForTranstionDismiss()
            
            imgView.frame = presentedDelegate.transitionPresentedStartFrame(indexPath)
            
            }) { (_) -> Void in
                
                imgView.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
    
    ///  modal 控制器的动画
    private func presentedTransition(transitionContext: UIViewControllerContextTransitioning){
        
        guard let presentedDelegate = presentedDelegate, indexPath = indexPath else{
            return
        }
        
        let imgView = presentedDelegate.imageViewForTranstionPresented(indexPath)
        transitionContext.containerView()?.addSubview(imgView)
        
        imgView.frame = presentedDelegate.transitionPresentedStartFrame(indexPath)
        
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        transitionContext.containerView()?.addSubview(toView)
        
        toView.alpha = 0
        
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as! CXLPhotoBrowserViewController
        
        toVC.collectionView.hidden = true
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            imgView.frame = presentedDelegate.transitionPresentedEndFrame(indexPath)
            
            toView.alpha = 1.0
            
            }) { (_) -> Void in
                
                imgView.removeFromSuperview()
                
                toVC.collectionView.hidden = false
                
                ///  必须实现,告诉控制器,动画已经完成
                transitionContext.completeTransition(true)
        }
    }
}